package avicit.bdp.dgs.standard.rest;

import avicit.bdp.common.dto.approval.ApprovalDTO;
import avicit.bdp.common.service.dto.CommonTypeDTO;
import avicit.bdp.common.utils.BdpLogUtil;
import avicit.bdp.core.constant.Constants;
import avicit.bdp.dgs.standard.dto.DataElementDto;
import avicit.bdp.dgs.standard.dto.DataElementHistoryDto;
import avicit.bdp.dgs.standard.service.DataElementHistoryService;
import avicit.bdp.dgs.standard.service.DataElementService;
import avicit.bdp.dgs.standard.vo.DataElementDistributionVo;
import avicit.bdp.dgs.standard.vo.DataElementHistoryDiffVo;
import avicit.bdp.dgs.standard.vo.DataElementHistoryVo;
import avicit.platform6.core.properties.PlatformConstant;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import com.alibaba.fastjson2.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author mayi 码表管理
 */
@RestController
@RequestMapping("/api/bdp/dgs/dataStandard/dataElement/")
@Api(tags = "数据项管理")
public class DataElementRest {

  private final DataElementService dataElementService;
  private final DataElementHistoryService dataElementHistoryService;

  @Autowired
  public DataElementRest(
      DataElementService dataElementService, DataElementHistoryService dataElementHistoryService) {
    this.dataElementService = dataElementService;
    this.dataElementHistoryService = dataElementHistoryService;
  }

  @ApiOperation(value = "分页查询")
  @ApiImplicitParams({
    @ApiImplicitParam(value = "分类ID", name = "typeId", dataType = "String", paramType = "query"),
    @ApiImplicitParam(value = "查询关键词", name = "keyWords", dataType = "String", paramType = "query"),
    @ApiImplicitParam(value = "状态", name = "status", dataType = "Int", paramType = "query"),
    @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int", paramType = "query"),
    @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
  })
  @GetMapping
  public ResponseMsg<QueryRespBean<DataElementDto>> getPageList(
      @RequestParam(required = false) String typeId,
      @RequestParam(required = false) String keyWords,
      @RequestParam(required = false) Integer status,
      @RequestParam(defaultValue = "1") Integer pageNo,
      @RequestParam(defaultValue = "10") Integer pageSize)
      throws Exception {

    ResponseMsg<QueryRespBean<DataElementDto>> responseMsg = new ResponseMsg<>();
    QueryRespBean<DataElementDto> queryRespBean =
        dataElementService.getPageList(typeId, keyWords, status, pageNo, pageSize);
    responseMsg.setResponseBody(queryRespBean);
    return responseMsg;
  }

  @ApiOperation(value = "新增")
  @PostMapping
  public ResponseMsg<String> save(@RequestBody DataElementDto dataElementDto) {
    ResponseMsg<String> responseMsg = new ResponseMsg<>();
    dataElementService.save(dataElementDto);
    responseMsg.setResponseBody(dataElementDto.getId());
    return responseMsg;
  }

  @ApiOperation(value = "修改")
  @PutMapping("/update")
  public ResponseMsg<Boolean> update(@RequestBody DataElementDto dataElementDto) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    dataElementService.update(dataElementDto);
    responseMsg.setResponseBody(true);
    return responseMsg;
  }

  @ApiOperation(value = "修订")
  @PutMapping("/amend")
  public ResponseMsg<String> amend(@RequestBody DataElementDto dataElementDto) {
    ResponseMsg<String> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(dataElementService.amend(dataElementDto));
    return responseMsg;
  }

  @ApiOperation(value = "批量删除")
  @ApiParam(value = "逗号分隔的id串", name = "ids", required = true)
  @DeleteMapping
  public ResponseMsg<Boolean> batchDelete(@RequestBody String ids) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(dataElementService.batchDelete(ids));
    return responseMsg;
  }

  @ApiOperation(value = "通过ID查询")
  @ApiParam(value = "id", name = "id", required = true)
  @GetMapping("/{id}")
  public ResponseMsg<DataElementDto> queryById(@PathVariable String id) {
    ResponseMsg<DataElementDto> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(dataElementService.queryById(id));
    return responseMsg;
  }

  @ApiOperation(value = "通过ID和版本查询")
  @ApiImplicitParams({
    @ApiImplicitParam(
        value = "数据元ID",
        name = "dataElementId",
        dataType = "String",
        paramType = "path"),
    @ApiImplicitParam(value = "版本", name = "version", dataType = "String", paramType = "path")
  })
  @GetMapping("/{dataElementId}/{version}")
  public ResponseMsg<DataElementHistoryDto> selectByDataElementIdAndVersion(
      @PathVariable String dataElementId, @PathVariable String version) {
    ResponseMsg<DataElementHistoryDto> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(
        dataElementHistoryService.selectByDataElementIdAndVersion(dataElementId, version));
    return responseMsg;
  }

  @ApiOperation(value = "发布")
  @ApiParam(value = "逗号分隔的id串", name = "ids", required = true)
  @PostMapping("/publish")
  public ResponseMsg<Boolean> publish(@RequestBody String ids) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    if (StringUtils.isNotBlank(ids)) {
      responseMsg.setResponseBody(dataElementService.publish(ids.split(Constants.COMMA)));
      String logTitle = "发布数据项标准：【" + JSONObject.toJSONString(ids) + "】";
      BdpLogUtil.log("数据项标准模块", logTitle, PlatformConstant.OpType.update);
    }
    return responseMsg;
  }

  @ApiOperation(value = "废止")
  @ApiParam(value = "数据元历史版本记录ID", name = "id", required = true)
  @GetMapping("/offline")
  public ResponseMsg<Boolean> offline(@RequestParam String id) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    if (StringUtils.isNotBlank(id)) {
      dataElementService.offline(id);
    }
    responseMsg.setResponseBody(true);
    return responseMsg;
  }

  @ApiOperation(value = "撤回")
  @ApiParam(value = "逗号分隔的id串", name = "ids", required = true)
  @PostMapping("/revoke")
  public ResponseMsg<Boolean> revoke(@RequestBody String ids) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    if (StringUtils.isNotBlank(ids)) {
      responseMsg.setResponseBody(dataElementService.revoke(ids.split(Constants.COMMA)));
    }
    return responseMsg;
  }

  /** 接收审批中心通知 */
  @PostMapping("/receiveMsg")
  @ApiOperation(value = "接收审批中心通知")
  public ResponseMsg<Boolean> receiveMsgFromApprovalCenter(
      @RequestBody List<ApprovalDTO> approvalList) {

    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(this.dataElementService.receiveMsgFromApprovalCenter(approvalList));
    return responseMsg;
  }

  @ApiOperation(value = "分页查询版本")
  @ApiImplicitParams({
    @ApiImplicitParam(
        value = "数据元ID",
        name = "dataElementId",
        dataType = "String",
        paramType = "query"),
    @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int", paramType = "query"),
    @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
  })
  @GetMapping("/versions")
  public ResponseMsg<QueryRespBean<DataElementHistoryVo>> getVersionPage(
      @RequestParam String dataElementId,
      @RequestParam(defaultValue = "1") Integer pageNo,
      @RequestParam(defaultValue = "10") Integer pageSize) {

    ResponseMsg<QueryRespBean<DataElementHistoryVo>> responseMsg = new ResponseMsg<>();
    QueryRespBean<DataElementHistoryVo> queryRespBean =
        dataElementService.getVersionPage(dataElementId, pageNo, pageSize);
    responseMsg.setResponseBody(queryRespBean);
    return responseMsg;
  }

  @ApiOperation(value = "版本比对")
  @ApiImplicitParams({
    @ApiImplicitParam(
        value = "当前版本ID",
        name = "currentVersionId",
        dataType = "String",
        paramType = "query",
        required = true),
    @ApiImplicitParam(
        value = "对比版本ID",
        name = "targetVersionId",
        dataType = "String",
        paramType = "query",
        required = true)
  })
  @GetMapping("/diff")
  public ResponseMsg<DataElementHistoryDiffVo> diff(
      @RequestParam String currentVersionId, @RequestParam String targetVersionId) {
    ResponseMsg<DataElementHistoryDiffVo> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(dataElementHistoryService.diff(currentVersionId, targetVersionId));
    return responseMsg;
  }

  @ApiOperation(value = "查询树状结构数据")
  @ApiImplicitParams({
    @ApiImplicitParam(value = "查询关键字", name = "keyWords", dataType = "String"),
    @ApiImplicitParam(value = "数据空间id", name = "projectId", dataType = "String")
  })
  @GetMapping("/getTreeList")
  public ResponseMsg<List<CommonTypeDTO>> getTreeList(
      @RequestParam(required = false) String keyWords,
      @RequestParam(required = false) String projectId)
      throws Exception {

    ResponseMsg<List<CommonTypeDTO>> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(dataElementService.getTreeList(projectId, keyWords));
    return responseMsg;
  }

  /** 下载模板 */
  @ApiOperation(value = "下载模板")
  @GetMapping("/downloadTemplate")
  public void downloadTemplate(HttpServletResponse response) throws Exception {
    dataElementService.downloadTemplate(response);
  }

  /** 导出数据元信息 */
  @ApiOperation(value = "导出")
  @ApiImplicitParams({
    @ApiImplicitParam(value = "分类id", name = "typeId", dataType = "String"),
    @ApiImplicitParam(value = "ID列表，逗号分割", name = "ids", dataType = "String")
  })
  @GetMapping("/export")
  public void export(
      @RequestParam(required = false, name = "typeId") String typeId,
      @RequestParam(required = false, name = "ids") String ids,
      HttpServletResponse response)
      throws Exception {

    List<String> idList = null;
    if (StringUtils.isNotBlank(ids)) {
      idList = Arrays.asList(ids.split(Constants.COMMA));
    }
    dataElementService.export(typeId, idList, response);
  }

  /** 导入数据元信息 */
  @ApiOperation(value = "导入")
  @PostMapping("/import")
  public ResponseMsg<String> importDataElement(@RequestParam(value = "file") MultipartFile file)
      throws Exception {

    ResponseMsg<String> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(dataElementService.importDataElement(file));
    return responseMsg;
  }

  @ApiOperation(value = "查询标准下发列表")
  @ApiImplicitParams({
    @ApiImplicitParam(
        value = "数据项ID",
        name = "dataElementId",
        dataType = "String",
        paramType = "query"),
    @ApiImplicitParam(
        value = "数据源",
        name = "dataSourceId",
        dataType = "String",
        paramType = "query"),
    @ApiImplicitParam(
        value = "元数据ID",
        name = "metadataId",
        dataType = "String",
        paramType = "query"),
          @ApiImplicitParam(value = "查询关键词", name = "keyWords", dataType = "String", paramType = "query"),
    @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int", paramType = "query"),
    @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
  })
  @GetMapping("/distribute/list")
  public ResponseMsg<QueryRespBean<DataElementDistributionVo>> getDistributionList(
      @RequestParam(required = false) String dataElementId,
      @RequestParam(required = false) String dataSourceId,
      @RequestParam(required = false) String metadataId,
      @RequestParam(required = false) String keyWords,
      @RequestParam(defaultValue = "1") Integer pageNo,
      @RequestParam(defaultValue = "10") Integer pageSize)
      throws Exception {

    ResponseMsg<QueryRespBean<DataElementDistributionVo>> responseMsg = new ResponseMsg<>();
    QueryRespBean<DataElementDistributionVo> queryRespBean =
        dataElementService.getDistributionList(
            dataElementId, dataSourceId, metadataId, keyWords, pageNo, pageSize);
    responseMsg.setResponseBody(queryRespBean);
    return responseMsg;
  }

  @ApiOperation(value = "标准下发")
  @ApiImplicitParam(
      value = "元数据列ID数组",
      name = "metadataColumnList",
      dataType = "List",
      paramType = "body")
  @PostMapping("/{dataElementId}/distribute/add")
  public ResponseMsg<Boolean> dataElementDistribute(
      @PathVariable String dataElementId, @RequestBody List<String> metadataColumnList) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(
        dataElementService.dataElementDistribute(dataElementId, metadataColumnList));
    return responseMsg;
  }

  @ApiOperation(value = "标准下发移除")
  @ApiImplicitParam(
      value = "元数据列ID数组",
      name = "metadataColumnList",
      dataType = "List",
      paramType = "body")
  @PostMapping("/{dataElementId}/distribute/remove")
  public ResponseMsg<Boolean> dataElementDistributeRemove(
      @PathVariable String dataElementId, @RequestBody List<String> metadataColumnList) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(
        dataElementService.dataElementDistributeRemove(dataElementId, metadataColumnList));
    return responseMsg;
  }
}
